package com.codingke.codingbookstore.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.allthelucky.common.view.AutoPlayManager;
import com.allthelucky.common.view.ImageIndicatorView;
import com.codingke.codingbookstore.R;
import com.codingke.codingbookstore.adapter.BookListItemAdapter;
import com.codingke.codingbookstore.ui.BookDetailActivity;
import com.codingke.codingbookstore.ui.LoginActivity;
import com.codingke.codingbookstore.ui.MainActivity;
import com.codingke.codingbookstore.ui.ShoppingCartActivity;
import com.codingke.codingbookstore.vo.BookInfo;
import com.codingke.codingbookstore.vo.User;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;

/**
 * descreption:
 * company: codingke.com
 * Created by vince on 15/11/13.
 */
public class HomeFragment extends BaseFragment implements AdapterView.OnItemClickListener,View.OnClickListener{

    private ImageIndicatorView indicate_view;
    private ListView listView;
    private View loading_data;
    private MainActivity mainActivity;
    private ArrayList<BookInfo> bookInfos = new ArrayList<>();
    private BookListItemAdapter bookListItemAdapter;
    private ImageView imageView_shopping_cart;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mainActivity = (MainActivity) getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home,null);
        loading_data = view.findViewById(R.id.loading_data);
        imageView_shopping_cart = (ImageView) view.findViewById(R.id.imageView_shopping_cart);
        imageView_shopping_cart.setOnClickListener(this);
        //loading_data.setVisibility(View.GONE);
        initIndicateView(view);
        listView = (ListView) view.findViewById(R.id.listView);
        bookListItemAdapter = new BookListItemAdapter(mainActivity,bookInfos);
        listView.setAdapter(bookListItemAdapter);
        listView.setOnItemClickListener(this);

        loadData();
        return view;
    }

    //初始化主页列表数据
    private void loadData() {
        if(bookInfos.size()==0) {
            Log.i(this.getClass().getName(),"load data");
            loading_data.setVisibility(View.VISIBLE);
            listView.setVisibility(View.GONE);
            BmobQuery<BookInfo> query = new BmobQuery<>();
            //query.setCachePolicy(BmobQuery.CachePolicy.CACHE_ELSE_NETWORK);//先从缓存中取数据,没有再从网络上取
            query.addWhereEqualTo("star",5);//查询推荐为5星的图书
            query.findObjects(mainActivity, new FindListener<BookInfo>() {
                @Override
                public void onSuccess(List<BookInfo> list) {
                    bookInfos = (ArrayList<BookInfo>) list;
//                    System.out.println(bookInfos);
                    bookListItemAdapter.setBookInfos(bookInfos);
                    bookListItemAdapter.notifyDataSetChanged();
                    loading_data.setVisibility(View.GONE);
                    listView.setVisibility(View.VISIBLE);
                }

                @Override
                public void onError(int i, String s) {
                    loading_data.setVisibility(View.GONE);
                    listView.setVisibility(View.VISIBLE);
                }
            });
        }
    }

    private void initIndicateView(View view) {
        indicate_view = (ImageIndicatorView) view.findViewById(R.id.indicate_view);
        final Integer[] resArray = new Integer[] { R.mipmap.qdzt, R.mipmap.qinghuabiancheng,R.mipmap.scala};
        indicate_view.setupLayoutByDrawable(resArray);
        indicate_view.setIndicateStyle(ImageIndicatorView.INDICATE_USERGUIDE_STYLE);
        indicate_view.show();
        AutoPlayManager autoBrocastManager =  new AutoPlayManager(indicate_view);
        autoBrocastManager.setBroadcastEnable(true);
        autoBrocastManager.setBroadCastTimes(5);//loop times
        autoBrocastManager.setBroadcastTimeIntevel(3 * 1000, 3 * 1000);//set first play time and interval
        autoBrocastManager.loop();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        BookInfo bookInfo = bookInfos.get(position);
        Intent intent = new Intent(mainActivity, BookDetailActivity.class);
        intent.putExtra("bookInfo",bookInfo);
        startActivity(intent);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.imageView_shopping_cart:
                //判断用户是否已经登录
                User user = BmobUser.getCurrentUser(mainActivity,User.class);
                if(user==null){
                    mainActivity.showToast("请登录后再查看购物车");
                    startActivity(new Intent(mainActivity,LoginActivity.class));
                    return;
                }
                startActivity(new Intent(mainActivity,ShoppingCartActivity.class));
                break;
        }
    }
}
